package com.googlecode.struts2tagsplugin.components;


import com.opensymphony.xwork2.util.ValueStack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.ClosingUIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@StrutsTag(name = "tooglepanel", tldTagClass = "com.googlecode.struts2tagsplugin.views.jsp.ui.TooglePanelTag", description = "Renders TooglePanel")
public class TooglePanel extends ClosingUIBean {

    public static final String TEMPLATE = "tagstooglepanel";
    public static final String TEMPLATE_CLOSE = "tagstooglepanel-close";

    final protected static Log LOG = LogFactory.getLog(TooglePanel.class);

    private String open;

    public TooglePanel(ValueStack stack, HttpServletRequest request,
                       HttpServletResponse response) {
        super(stack, request, response);
    }

    @Override
    public void evaluateParams() {
        super.evaluateParams();

        if (this.name != null)
            this.addParameter("name", this.findString(this.name));


        if (this.key != null) {
            String expr = key;
            if (altSyntax()) {
                expr = "%{" + expr + "}";
            }
            addParameter("nameValue", findString(expr));
        }

        if (this.open != null)
            addParameter("open", this.findValue(this.open,Boolean.class));
    }

    public String getDefaultOpenTemplate() {
        return TEMPLATE;
    }

    @Override
    protected String getDefaultTemplate() {
        return TEMPLATE_CLOSE;
    }

    @Override
    @StrutsTagAttribute(description = "Name", required = true)
    public void setName(String name) {
        this.name = name;
    }

    @Override
    @StrutsTagAttribute(description = "Key", required = true)
    public void setKey(String key) {
        this.key = key;
    }

    @StrutsTagAttribute(description = "Set it false if you want to make it non-visible", type = "Boolean", defaultValue = "true")
    public void setOpen(String open) {
        this.open = open;
    }
}
